﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Windows;

public partial class register : System.Web.UI.Page
{
    private int passHashCode;
    private string connectionString = WebConfigurationManager.ConnectionStrings["bugger"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack == false)
        {
            chkiagree.Checked = true;
            lblText.Visible = false;
        }
    }
   
    protected void chkiagree_ServerValidate(object source, ServerValidateEventArgs e)
    {
        e.IsValid = chkiagree.Checked;
    }
    protected void user_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        SqlConnection userConnection = new SqlConnection(connectionString);
        userConnection.Open();

        string sqlSelect = "SELECT username FROM users";
        SqlCommand SelectUser = new SqlCommand(sqlSelect, userConnection);
        SqlDataReader DataReader;
        DataReader = SelectUser.ExecuteReader();
        while (DataReader.Read())
        {
            if (txtusername.Text == DataReader["username"].ToString())
            {
                e.IsValid = false;
                break;
            }
        }
    }
    protected void email_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        SqlConnection userConnection = new SqlConnection(connectionString);
        userConnection.Open();

        string sqlSelect = "SELECT email FROM users";
        SqlCommand SelectEmail = new SqlCommand(sqlSelect, userConnection);
        SqlDataReader DataReader;
        DataReader = SelectEmail.ExecuteReader();
        while (DataReader.Read())
        {
            if (txtEmail.Text == DataReader["email"].ToString())
            {
                e.IsValid = false;
                break;
            }
        }
    }

    protected void btnregister_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SqlConnection userConnection = new SqlConnection(connectionString);
            userConnection.Open();

            //Generate hashcode to save password
            passHashCode = txtpassword.Text.GetHashCode();

            //Creates Insert Command for the database
            string sqlInsert = "INSERT INTO users(firstname, lastname,username,password,email,role,dateofjoining) ";
            sqlInsert += "VALUES(@firstname, @lastname, @username, @password, @email,@role,@dateofjoining)";
            SqlCommand InsertCommand = new SqlCommand(sqlInsert, userConnection);
            
            //Pass values to the parameters in Insert Command
            InsertCommand.Parameters.AddWithValue("@firstname", txtfirstname.Text);
            InsertCommand.Parameters.AddWithValue("@lastname", txtlastname.Text);
            InsertCommand.Parameters.AddWithValue("@username", txtusername.Text);
            InsertCommand.Parameters.AddWithValue("@password", passHashCode);
            InsertCommand.Parameters.AddWithValue("@email", txtEmail.Text);
            InsertCommand.Parameters.AddWithValue("@role", 1);
            InsertCommand.Parameters.AddWithValue("@dateofjoining", DateTime.Now.ToShortDateString());

            //Execute Insert Command and check if it was a success or not
            if (InsertCommand.ExecuteNonQuery() != 0)
            {
                Response.Redirect("login.aspx");
            }
            else
                lblText.Text = "Registration Failed!";
            userConnection.Close();
        }
    }
}